<?php
include './inc/PAGE.php';
$p = new PAGE("");

$loginOk = $p->s()->getvar("loginOk");
$username = $p->s()->getvar("username");
$type = $p->s()->getvar("type");
if($loginOk==1  && strlen($username)>0) {
    $p = new PAGE("Αναζήτηση");
}else {
    header("Location: ./index.php");
}


$selectbook = $_GET['selectbook'];
$searchterm = $p->t()->cleaner($_POST['searchterm']);
$byterm = $p->t()->cleaner($_POST['byterm']);

$wheresql = "";
if(strlen($searchterm)>0) {
    $wheresql = " WHERE ".$byterm." LIKE '%".$searchterm."%'";
    $yousearchedfor = "Αναζήτηση για '<b>".$searchterm."</b>' στην κατηγορία '<i>".$byterm."</i>':";
}

if($type==USER) {
    $searchdisabled = "";
}else {
    $searchdisabled = "disabled";
}
/*
$p->t()->debug($selectbook);
$p->t()->debug($searchterm);
$p->t()->debug($byterm);
$p->t()->debug($wheresql);

*/
?>
<script type="text/javascript">
    function apply(){
        var d = document.borrow_book.selectbook;
        var i, length = d.length, report="TODO functionality\n===========\n\n";

        for(i=0;i< length;i++){
            if(d[i].checked){
                report = report+"id "+d[i].value+" is selected\n";
            }
        }
        alert(report);
        return true;
    }
</script>

<table width="100%" class="tableempty">
    <tr>
        <td>
            <?php
            if($type==LIB) {
                echo "<a href=\"ze_book_add.php\">Προσθήκη βιβλίου[+]</a>";
            }
            echo "&nbsp;&nbsp;".$yousearchedfor;
            ?>
        </td>
        <td align="right">
            <form action="ze_book_search.php" method="POST">
                <input type="text" name="searchterm" value="" />
                <select name="byterm" >
                    <option>name</option>
                    <option>category</option>
                    <option>writer</option>
                    <option>details</option>
                    <option>type</option>
                    <option>ISBN</option>
                </select>
                <input type="submit" value="Αναζήτηση" />
            </form>
        </td>
    </tr>
</table>

<form action="ze_book_borrow.php" method="get" name="borrow_book">
    <table border="0" cellpadding="3" class="tableform" width="100%">
        <thead>
            <tr>
                <th> </th>
                <th align="center">Κατηγορία</th>
                <th align="center">Τίτλος</th>
                <th align="center">Συγγραφέας</th>
                <th align="center">Λεπτομέρειες</th>
                <th align="center">Τύπος</th>
                <th align="center">ISBN</th>
                <th align="center">Αντίγραφα</th>
                <th align="center">Διαθέσιμα για δανεισμό</th>
                <th align="center">Επιλογή</th>
                <?php
                if($type==LIB) {
                    echo "<th align=\"center\">Διαγραφή</th>";
                }
                ?>
            </tr>
        </thead>
        <tbody>


            <?php
            $sql = "SELECT id,category,name,writer,details,type,ISBN,copies FROM books".$wheresql;
            $array = $p->db()->resultasarray($sql);
            for($i=0;$i<count($array);$i++) {
                echo "<tr>\n";
                for($j=0;$j<count($array[0]);$j++) {
                    if(strlen($array[$i][$j])>100) {
                        $array[$i][$j] = substr($array[$i][$j],0,100)."[...]";
                    }
                    echo "<td class=\"top\">".$array[$i][$j]."</td>\n";
                }
                echo "<td></td>";
                echo "<td align=center ><input type=\"checkbox\" name=\"selectbook[]\" value=\"".$array[$i][0]."\"/></td>\n";

                if($type==LIB) {
                    echo "<td align=\"center\"><a onclick='return yesorno(\"Είστε σίγουροι; Δεν μπορεί να ανερεθεί η ενέργεια!\");' href='ze_book_delete.php?id=".$array[$i][0]."'>[-]</a></td>";
                }
                echo "</tr>\n";
            }
            ?>
        </tbody>
    </table>
    <div align="right"><input type="submit" value="Δανεισμός" <?=$searchdisabled?>/></div>
</form>



<?
$p->close();
?>